<template>
  <el-dialog v-dialogDrag :visible.sync="dialog" width="780px" :close-on-click-modal="false" :before-close="reset">
    <span slot="title" class="dialog-title">分配账号</span>
    <div class="dialoginfo">
      <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width: 100%" :data="tableData" tooltip-effect="dark">
        <el-table-column align="center" label="角色" prop="roleName"></el-table-column>
        <el-table-column align="center" label="人员">
          <template slot-scope="scope">
            <el-select v-model="scope.row.list1" placeholder="请选择" multiple filterable>
              <el-option v-for="item in scope.row.list" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button class="bottom_all_two" @click="reset">取 消</el-button>
      <el-button class="bottom_all" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {  contractUser, getListByContractId, get_simple_user_listBycompanyId, supplierQueryOneByid,
  clientGet_function,
  judeJurisdiction,} from '@/api/GHnumber/contract'
class saveDate {
  constructor(contractId, userId, userName, roleId) {
    this.contractId = contractId
    this.userId = userId
    this.userName = userName
    this.roleId = roleId
  }
}
let oldlist = []
export default {
  props: {
    dialog: {
      type: Boolean,
      default: false
    },
    low: {//是合同id
      type: String,
      default: ''
    },
    supplierId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      tableData: [
        { roleName: '接单员', roleId: '7331806d50c74bb6bf033886a4468603', list: [], list1: [] },
        { roleName: '签票员', roleId: 'e0f2599ee6c840a58213af21e2a46a76', list: [], list1: [] },
        { roleName: '结算员', roleId: '560bedbaf7b64913b514e8ea96aeca9e', list: [], list1: [] },
        { roleName: '异常处理员', roleId: 'f66da93bd8634217bdfba8028678f1b4', list: [], list1: [] },
      ],
    }
  },
  watch: {
    dialog() {
      if (this.dialog) {
        this.getTable()
      }
    }
  },
  methods: {
    getTable() {
      supplierQueryOneByid({ id: this.supplierId }).then((res0) => {
        this.tableData.forEach((res) => {
          get_simple_user_listBycompanyId({ roleId: res.roleId, orgznizationId: res0.data.externalCompanyId }).then((item) => {
            res.list = item.data
          })
        })
        getListByContractId({ contractId: this.low }).then((res) => {
          oldlist = JSON.parse(JSON.stringify(res.data))
          this.tableData.forEach(item => {
            item.list1 = res.data.filter(item1 => (item.roleId == item1.roleId)).map(res => (res.userId))
          })
        })
      })

    },
    reset() {
      this.$emit('reset', 'dialog1')
    },
    submit() {
      const setArr = (pre, cur) => {
        let arr = cur.list1.map((res) => {
          const data = cur.list.find(item => (item.id == res))
          return new saveDate(this.low, data.id, data.name, cur.roleId)
        })
        return [...arr, ...pre]
      }
      let userList = this.tableData.reduce(setArr, [])
      let allHasData = oldlist.filter((item) => (
        userList.some((items) => (item.roleId == items.roleId && item.userId == items.userId))
      ))
      let deleteData = oldlist.filter((item) => (
        !allHasData.some((items) => (item.roleId == items.roleId && item.userId == items.userId))
      )).map(res => ({ contractId: res.contractId, userName: res.userName, userId: res.userId, roleId: res.roleId, id: res.id, status: 0 }))
      let addData = userList.filter((item) => (
        !allHasData.some((items) => (item.roleId == items.roleId && item.userId == items.userId))
      ))
      contractUser([...addData, ...deleteData]).then((res) => {
        if (res.code == 1001) {
          this.$message({
            message: '保存成功',
            type: 'success'
          });
          this.$emit('submit', 'dialog1')
        }
      })

      //验证是否有权限功能，仅提示---开始
      let list = []
      console.log(userList);
      userList.forEach((v) => {
        list.push({ id: v.userId, name: v.userName, type: 1 })//1：PC；2：移动端；3：微信小程序；4：首页
      })
      clientGet_function(list).then((res) => {
        let json = {
          id: this.low,//工程id，合同的分配传合同id
          status: 1,//1 客户 2 供应商
          jurisdictionList: res.data
        }
        judeJurisdiction(json).then((res) => {
          console.log(res);
          let div = ``
          res.data.forEach((v) => {
            div += `<div style="word-break: break-all;word-wrap: break-word;font-weight: 100;">` + v + `</div>`
          })
          this.$notify({
            title: '温馨提示',
            dangerouslyUseHTMLString: true,
            message: '<strong>' + div + '</strong>',
            position: 'bottom-right',
            duration: 0
          });
        })
      })
      //验证是否有权限功能，仅提示---结束
    },
  }
}
</script>

